package draftMate;

import java.util.ArrayList;

import draftMate.player.Hitter;
import draftMate.player.Pitcher;
import draftMate.PlayerNotFoundException;

public class Team {
        public static final int NUM_FIELDS = 11;
        public static final int ROW_SIZE = 17;
        public static final int DEF_NUM_ARMS = 10;
        public static final int DEF_NUM_BATS = 10;
        
        public static final int NAME = 0;
        public static final int AVG = 1;
        public static final int R = 2;
        public static final int RBI = 3;
        public static final int HR = 4;
        public static final int SB = 5;
        public static final int W = 6;
        public static final int ERA = 7;
        public static final int WHIP = 8;
        public static final int K = 9;
        public static final int SV = 10;
        
        private static final int IP = 11;
        private static final int PH = 12; // pitcher hits
        private static final int BB = 13;
        private static final int ER = 14;
        private static final int AB = 15;
        private static final int HH = 16; // hitter hits
    
        private Object [] row;
	private ArrayList<Pitcher> arms;
	private ArrayList<Hitter> bats;
		
	public Team(String name) {
            row = new Object[ROW_SIZE];
            arms = new ArrayList<Pitcher>(DEF_NUM_ARMS);
            bats = new ArrayList<Hitter>(DEF_NUM_BATS);
            row[NAME] = name;
         
            for (int i = 1; i < ROW_SIZE; i++) {
                row[i] = 0.0;
            } // init all fields to 0;
        }
	public void addPitcher(Pitcher pitcher) {
		arms.add(pitcher);
		
		// first add all count stats
		
		row[IP] =  (double)row[IP] + pitcher.getIp();
		row[PH] = (double)row[PH] + pitcher.getHits();
		row[BB] = (double)row[BB] + pitcher.getWalks();
		row[ER] = (double)row[ER] + pitcher.getER();
		row[W] = (double)row[W] + pitcher.getWins();
		row[K] = (double)row[K] + pitcher.getK();
		row[SV] = (double)row[SV] + pitcher.getSaves();
		
		// then re calculate all the avg type stats
		
		row[ERA] = ((double)row[ER] * 9) / (double)row[IP];
		row[WHIP] = ((double)row[BB] + (double)row[PH]) / (double)row[IP];
	}
	
        /**
         * 
         * @param index index of pitcher to remove
         * @return returns the pitcher removed
         */
	public Pitcher removePitcher(int index) {
		Pitcher toDrop = arms.remove(index);
		
		// first subtract all count stats
                
		row[IP] = (double)row[IP] - toDrop.getIp();
		row[PH] = (double)row[PH] - toDrop.getHits();
		row[BB] = (double)row[BB] - toDrop.getWalks();
		row[ER] = (double)row[ER] - toDrop.getER();
		row[W] = (double)row[W] - toDrop.getWins();
		row[K] = (double)row[K] - toDrop.getK();
		row[SV] = (double)row[SV] - toDrop.getSaves();
		
		// then re calculate all the avg type stats
		
                if (row[IP].equals(0.0)) {
                    row[ERA] = row[WHIP] = 0.0;
                } else {
                    row[ERA] = ((double)row[ER] * 9) / (double)row[IP];
                    row[WHIP] = ((double)row[BB] + (double)row[BB]) / (double)row[IP];
                }
		
		return toDrop;
	}
	
        /**
         * adds hitter to team
         * @param hitter hitter to add to team
         */
	public void addHitter(Hitter hitter) {
		bats.add(hitter);
		
		row[AB] = (double)row[AB] + hitter.getAb();
		row[HH] = (double)row[HH] + hitter.getHits();
		row[HR] = (double)row[HR] + hitter.getHr();
		row[R] = (double)row[R] + hitter.getRuns();
		row[RBI] = (double)row[RBI] + hitter.getRbi();
		row[SB] = (double)row[SB] + hitter.getSteals();
		
		row[AVG] = (double)row[HH] / (double)row[AB];
	}
	
        /**
         * 
         * @param name name of player to remove
         * @return the hitter just removed from team
         * @throws PlayerNotFoundException if hitter is not on team
         */
	public Hitter removeHitter(int index) {
		Hitter toDrop = bats.remove(index);
		
		row[AB] = (double)row[AB] - toDrop.getAb();
		row[HH] = (double)row[HH] - toDrop.getHits();
		row[HR] = (double)row[HR] - toDrop.getHr();
		row[R] = (double)row[R] - toDrop.getRuns();
		row[RBI] = (double)row[RBI] - toDrop.getRbi();
		row[SB] = (double)row[SB] - toDrop.getSteals();
		
                if (row[AB].equals(0.0)) {
                    row[AVG] = 0.0;
                } else {
                    row[AVG] = (double)row[HH] / (double)row[AB];
                }
                
                return toDrop;
	}
        
        /**
         * returns an array of strings containing the teams name and
         * projected stats
         * @return string array containing the teams name and stats
         */
        public Object getValAt(int c) {
            return row[c];
        }
        
        public Hitter getHitter(int index) {
            return bats.get(index);
        }
        
        public Pitcher getPitcher(int index) {
            return arms.get(index);
        }
        
        public int getNumHitters() {
            return bats.size();
        }
        
        public int getNumPitchers() {
            return arms.size();
        }
        
        public String getName() {
            return (String)row[NAME];
        }
        
        public double getpIP() {
            return (double)row[IP];
        }
        
        public double getpHitsP() {
            return (double)row[PH];
        }
        
        public double getpWalks() {
            return (double)row[BB];
        }
        
        public double getpER() {
            return (double)row[ER];
        }
        
        public double getpWins() {
            return (double)row[W];
        }
        
        public double getpERA() {
            return (double)row[ERA];
        }

	public double getpWHIP() {
            return (double)row[WHIP];
	}

	public double getpK() {
            return (double)row[K];
	}

	public double getpSaves() {
            return (double)row[SV];
	}

	public double getpAB() {
            return (double)row[AB];
	}

	public double getpHitsH() {
            return (double)row[HH];
	}

	public double getpHR() {
            return (double)row[HR];
	}

        public double getpRuns() {
            return (double)row[R];
        }
        
        public double getpRBI() {
            return (double)row[RBI];
        }
        
        public double getpSteals() {
            return (double)row[SB];
        }
        
        public double getAvg() {
            return (double)row[AVG];
        }
}
